
import com.payne.reader.Reader;
import com.payne.reader.bean.config.AntennaCount;
import com.payne.reader.bean.config.MemBank;
import com.payne.reader.bean.send.ReadConfig;
import com.payne.reader.process.ReaderImpl;
import com.library.ezd.handle.JSerialPortHandle;

import java.util.Arrays;

/**
 * @Package: PACKAGE_NAME
 * @ClassName: Connection
 * @Author: Yin
 * @Date: 11:25
 * @Description: 此文件描述信息
 */
public class Connection {

    public static void main(String[] args) throws InterruptedException {
        // 读写器 （模块天线口数量 4 ）
        Reader reader = ReaderImpl.create(AntennaCount.FOUR_CHANNELS);

        // 网口
//        NetworkHandle networkHandle = new NetworkHandle("127.0.0.1",4001);
        // 串口
        JSerialPortHandle serialPortHandle = new JSerialPortHandle("COM4",115200);

        boolean connect = reader.connect(serialPortHandle);
        if(connect) {
            // 读标签 TID 标签
            ReadConfig.Builder builder = new ReadConfig.Builder();
            builder.setMemBank(MemBank.TID);
            builder.setWordLength((byte) 6);
            builder.setWordStartAddress((byte) 0);
            builder.setPasswords(new byte[]{0,0,0,0});

            ReadConfig readConfig = builder.build();
            System.out.println(Arrays.toString(readConfig.getReadInfo()));
            while(true) {
                try{
                    Thread.sleep(300);
                }catch (Exception e){}
                reader.readTag(readConfig
                    , (suc) -> {
                        System.out.println("start---------------");
                        System.out.println(suc.getCmd());
                        System.out.println(suc.getFreq());
                        System.out.println(suc.getAntId());
                        System.out.println(suc.getStrPc());
                        System.out.println("信号：" + suc.getRssi());
                        System.out.println("数据长度：" + suc.getDataLen());
                        System.out.println("数据：" + suc.getStrData());
                        System.out.println("标签数：" + suc.getTagCount());
                        System.out.println("raw data："+ Arrays.toString(suc.getRawData()));
                        System.out.println("end---------------");
                    }, (fail) -> {
                        System.out.println("start---------------");
                        System.out.println(fail.getCmd());
                        System.out.println(fail.getErrorCode());
                        System.out.println("end---------------");
                    }
                );
            }
        }else {
            System.out.println("连接失败");
        }

    }

}
